home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 2 / Amiga Tools 2.iso / grafik / bildanzeiger / egsflick / read.c < prev    next >
C/C++ Source or Header  |  1995-03-09  |  14KB  |  574 lines

  1. /*
  2.             EGSFlick - Frank Neumann, November 1993
  3.  
  4.             This file, read.c, was taken over from the original xflick
  5.             source almost without any modifications - except for these 
  6.             comments, the include file change below, and a call to the
  7.             new error/exit routine 'Crash' instead of 'xferror'.
  8.             Here is the original comment:
  9.  
  10.                xflick - Ron Schnell, March, 1991
  11.  
  12.            This code is provided as is, with no warrantees, expressed
  13.                or implied.  I believe this code to be free of encumbrance,
  14.            and offer it to the public domain.  I ask, however, that
  15.            this paragraph and my name be retained in any modified
  16.            versions of the file you may make, and that you notify me
  17.            of any improvements you make to the code.
  18.  
  19.            Ron Schnell (ronnie@sos.com)
  20.  
  21.  
  22.             The following changes are from Michael Pall
  23.             (pall@rz.uni-karlsruhe.de) Mar 25-28 1991:
  24.  
  25.             Lots of bugfixes and changes to the structure of the files.
  26.             The interpretation part is now in this file.
  27. */
  28.  
  29. /*    This is read.c, the functions which read in FLI structures in
  30.       a machine independent format.  */
  31.  
  32.  
  33. #include <stdio.h>
  34. #include <sys/types.h>
  35. #include "eflick.h"
  36.  
  37. #define TRUE 1
  38. #define FALSE 0
  39.  
  40. #define DEBUGGING 0
  41.  
  42. extern int verbose;
  43. extern void Crash( char *);
  44.  
  45. void
  46. read_flihead(fd, header)
  47. int fd;
  48. struct fli_header *header;
  49. {
  50.     unsigned char *buf = (unsigned char *)malloc(SIZE_HEADER);
  51.     unsigned char *sbf;
  52.     int i;
  53.  
  54.     sbf = buf;
  55.     (void) read(fd, buf, SIZE_HEADER);
  56.     header->fhd_size = get_long(buf);
  57.     buf += LONGSIZE;
  58.  
  59.     header->fhd_magic = get_short(buf);
  60.     buf += SHORTSIZE;
  61.  
  62.     header->fhd_frames = get_short(buf);
  63.     buf += SHORTSIZE;
  64.  
  65.     header->fhd_width = get_short(buf);
  66.     buf += SHORTSIZE;
  67.  
  68.     header->fhd_height = get_short(buf);
  69.     buf += SHORTSIZE;
  70.  
  71.     header->fhd_gap = get_short(buf);
  72.     buf += SHORTSIZE;   /* nothing */
  73.  
  74.     header->fhd_flags = get_short(buf);
  75.     buf += SHORTSIZE;   /* flags */
  76.  
  77.     header->fhd_speed = get_short(buf);
  78.     buf += SHORTSIZE;
  79.  
  80.     header->fhd_next = get_long(buf);
  81.     buf += LONGSIZE;
  82.  
  83.     header->fhd_frit = get_long(buf);
  84.     buf += LONGSIZE;
  85.  
  86.     bcopy(buf, header->fhd_expand, FHD_EXPAND_SIZE);
  87.  
  88.     free(sbf);
  89.  
  90.     if (verbose) {
  91.       fprintf(stderr, "header->fhd_size: %d\n", header->fhd_size);
  92.       fprintf(stderr, "header->fhd_magic: 0x%x\n", header->fhd_magic);
  93.       fprintf(stderr, "header->fhd_frames: %d\n", header->fhd_frames);
  94.       fprintf(stderr, "header->fhd_width: %d\n", header->fhd_width);
  95.       fprintf(stderr, "header->fhd_height: %d\n", header->fhd_height);
  96.       fprintf(stderr, "header->fhd_gap: 0x%x\n", header->fhd_gap);
  97.       fprintf(stderr, "header->fhd_flags: 0x%x\n", header->fhd_flags);
  98.       fprintf(stderr, "header->fhd_speed: %d\n", header->fhd_speed);
  99.       fprintf(stderr, "header->fhd_next: %d\n", header->fhd_next);
  100.       fprintf(stderr, "header->fhd_frit: 0x%x\n", header->fhd_frit);
  101.       fprintf(stderr, "header->fhd_expand: ");
  102.       for (i = 0; i < FHD_EXPAND_SIZE; i++)
  103.     fprintf(stderr, "%x ", 0xff & (unsigned int)header->fhd_expand[i]);
  104.       fprintf(stderr, "\n");
  105.     }
  106. }
  107.  
  108. void
  109. read_fheader(fd, fheader)
  110. int fd;
  111. struct frame_header *fheader;
  112. {
  113.     unsigned char data[SIZE_FHEADER];
  114.     unsigned char *buf = data;
  115.     int ret;
  116.     unsigned char tmp;
  117.     int syncNotFound = TRUE;
  118.     int i;
  119.  
  120.     /* Syncronize to the magic word */
  121.     i = 0;
  122.     do {
  123.       ret = read(fd, &tmp, 1);
  124.       ++i;
  125.       if (verbose)
  126.     fprintf(stderr, "read1 i: %d tmp: 0x%x sync: %d\n",
  127.         i, 0xff & (unsigned int)tmp, syncNotFound);
  128.       if (tmp == (unsigned char)0xfa) {
  129.     ret = read(fd, &tmp, 1);
  130.     ++i;
  131.     if (tmp == (unsigned char)0xf1)
  132.       syncNotFound = FALSE;
  133.     else
  134.       syncNotFound = TRUE;
  135.     if (verbose)
  136.       fprintf(stderr, "read2 i: %d tmp: 0x%x sync: %d\n",
  137.           i, 0xff & (unsigned int)tmp, syncNotFound);
  138.       }
  139.     } while((syncNotFound == TRUE) && (ret > 0));
  140.  
  141.     if (ret < 0) {
  142.       fprintf(stderr,
  143. "Error reading input file looking for frame header magic number. i: %d\n", i);
  144.       exit(-1);
  145.     }
  146.  
  147.     if (verbose)
  148.       fprintf(stderr, "After Sync i: %d sync: %d ret: %d\n",
  149.           i, syncNotFound, ret);
  150.  
  151.     if ((ret = lseek(fd, -6, SEEK_CUR)) < 0) {
  152.       perror("Seeking to begining of frame");
  153.       exit(-1);
  154.     }
  155.  
  156.     (void) read(fd, buf, SIZE_FHEADER);
  157.  
  158.     fheader->fr_size = get_long(buf);
  159.     buf += LONGSIZE;
  160.     fheader->fr_magic = get_short(buf);
  161.     buf += SHORTSIZE;
  162.     fheader->fr_chunks = get_short(buf);
  163.     bcopy(buf, fheader->fr_expand, FR_EXPAND_SIZE);
  164.  
  165.  
  166.     if (verbose) {
  167.       fprintf(stderr, "fheader->fr_size: %d\n", fheader->fr_size);
  168.       fprintf(stderr, "fheader->fr_magic: 0x%x\n", fheader->fr_magic);
  169.       fprintf(stderr, "fheader->fr_chunks: %d\n", fheader->fr_chunks);
  170.       fprintf(stderr, "fheader->fr_expand: ");
  171.       for (i = 0; i < FR_EXPAND_SIZE; i++)
  172.     fprintf(stderr, "%d ", 0xff & (unsigned int)fheader->fr_expand[i]);
  173.       fprintf(stderr, "\n");
  174.     }
  175.  
  176. }
  177.  
  178. void
  179. read_chunk(fd, chunk)
  180. int fd;
  181. struct chunk_header *chunk;
  182. {
  183.     unsigned char data[SIZE_CHEADER];
  184.     unsigned char *buf = data;
  185.  
  186.     (void) read(fd, buf, SIZE_CHEADER);
  187.     chunk->ch_bytes = get_long(buf);
  188.     buf += LONGSIZE;
  189.     chunk->ch_type = get_short(buf);
  190.  
  191.     if (verbose) {
  192.       fprintf(stderr, "chunk->ch_bytes: %d\n", chunk->ch_bytes);
  193.       fprintf(stderr, "chunk->ch_type: %d\n", chunk->ch_type);
  194.     }
  195. }
  196.  
  197. void
  198. interpret_fli(fd,header,data,notfirst,func)
  199. int fd;            /* The file descriptor */
  200. struct fli_header *header;    /* The header of the file */
  201. unsigned char *data;    /* A pointer to the pixel data */
  202. int notfirst;        /* Is this not the first time through? */
  203. void (*func)();        /* The function that stores or displays the chunks */
  204. {
  205.     int i, j, s, l, k;            /* Random look/status ints */
  206.     short ndiff;        /* To hold the # lines changed from last frame */
  207.     struct frame_header fheader;    /* To hold each frame header */
  208.     struct chunk_header chunk;        /* To hold each chunk header */
  209.     unsigned char *buf=(unsigned char *)0;
  210.                     /* malloc'd space to hold each chunk */
  211.     int bufalloc;            /* Space already allocated for buf */
  212.     int skchange, skcount, sicount;  /* Various pixel/data index's */
  213.     unsigned char *cptr,*dptr,*eptr;     /* data pointers into *buf */
  214.     int npack;                /* Number of packets in this chunk */
  215.     unsigned char by, by0, by1;        /* To hold a byte */
  216.     int curx, cury;            /* The current x & y coords */
  217.     short wwidth, wheight;        /* Width and height of the window */
  218.     int topy,bottomy,leftx,rightx;  /* For optimizing FLI_LC chunks */
  219.  
  220.     static long twopos;            /* To keep the file offset of frame # 2 */
  221.  
  222.     wwidth = header->fhd_width;
  223.     wheight = header->fhd_height;
  224.  
  225.     for (i=0; i <= header->fhd_frames; i++)
  226.     {
  227.  
  228.     /* The following stuff ist only needed if we display the frames
  229.        while we interpret the file */
  230.  
  231.     /* If this is the first time through, we display frame # 1,
  232.        otherwise, we skip right to frame two */
  233.  
  234.     if ((notfirst) && (i==0))
  235.     {
  236.         (void) lseek(fd, twopos, 0);
  237.         continue;
  238.     }
  239.  
  240.     /* If this is the first time through and we are at frame #2,
  241.        save our file offset for the next loop through */
  242.  
  243.     if (!(notfirst) && (i==1))
  244.         twopos = lseek(fd, 0, 1);
  245.  
  246.     /* Each frame has a header */
  247.  
  248.     read_fheader(fd, &fheader);
  249.  
  250.     for (j=0; j < fheader.fr_chunks; j++)
  251.     {
  252.         /* Each chunk also has a header */
  253.         read_chunk(fd, &chunk);
  254.  
  255.         /* Malloc the space to hold whole chunk, unless it
  256.            is a COPY chunk. Malloc/Realloc only if necessary */
  257.  
  258.         if (chunk.ch_type != FLI_COPY) {
  259.         if (!buf)
  260.         {
  261.             buf = (unsigned char *)malloc(chunk.ch_bytes);
  262.             bufalloc = chunk.ch_bytes;
  263.         }
  264.         else
  265.         {
  266.             if (bufalloc < chunk.ch_bytes)
  267.             {
  268.             buf = (unsigned char *)realloc(buf, chunk.ch_bytes);
  269.             bufalloc = chunk.ch_bytes;
  270.             }
  271.         }
  272.           }
  273.  
  274.         /* A COPY chunk means 64k of uncompressed image data,
  275.            read it directly into the image */
  276.  
  277.         if (chunk.ch_type == FLI_COPY)
  278.         s = read(fd, data, wwidth*wheight);
  279.         else
  280.         s = read(fd, buf, chunk.ch_bytes -  6);
  281.  
  282.         if (s < 0) {
  283.         fprintf(stderr, "s: %d fd: %d data: 0x%x chunk.ch_bytes: %d\n",
  284.             s, fd, data, chunk.ch_bytes);
  285.         Crash("Error reading image data.");
  286.           }
  287.  
  288.         cptr = buf;
  289.  
  290.         switch (chunk.ch_type) {
  291.         case FLI_COPY:
  292.         (*func)(FLI_COPY, i, data, 0,0, 0,0, wwidth,wheight);
  293.         break;
  294.  
  295.         /* A BLACK chunk */
  296.  
  297.         case FLI_BLACK:
  298.         bzero(data, wwidth * wheight);
  299.         (*func)(FLI_BLACK, i, data, 0,0, 0,0, wwidth,wheight);
  300.         break;
  301.  
  302.         /* A COLOR chunk:  A compressed colormap to be stored or
  303.            changed */
  304.  
  305.         case FLI_256_COLOR:
  306.         npack = get_short(cptr);
  307.         cptr += SHORTSIZE;
  308.  
  309.         for (l=0; l < npack; l++)
  310.         {
  311.             skcount = *(cptr++);     /* # colors not to change */
  312.             skchange = *(cptr++);    /* # of colors to change */
  313.             if (!skchange)           /* (0 means all) */
  314.             skchange=MAXCOL;
  315.  
  316.             /* The colors are stored in the right 6 bits
  317.                of the byte.  We then convert it to a short. */
  318.  
  319.             (*func)(FLI_256_COLOR, i, cptr, skcount,
  320.                 skchange, 0,0, 0,0);
  321.             cptr += skchange * 3;
  322.         }
  323.         break;
  324.  
  325.         case FLI_COLOR:
  326.         npack = get_short(cptr);
  327.         cptr += SHORTSIZE;
  328.  
  329.         for (l=0; l < npack; l++)
  330.         {
  331.             skcount = *(cptr++);     /* # colors not to change */
  332.             skchange = *(cptr++);    /* # of colors to change */
  333.             if (!skchange)           /* (0 means all) */
  334.             skchange=MAXCOL;
  335.  
  336.             /* The colors are stored in the right 6 bits
  337.                of the byte.  We then convert it to a short. */
  338.  
  339.             (*func)(FLI_COLOR, i, cptr, skcount, skchange, 0,0, 0,0);
  340.             cptr += skchange * 3;
  341.         }
  342.         break;
  343.  
  344.  
  345.         /* Line compressed, and bytewise/run-length compressed
  346.            chunks are similar.  BRUN chunks always start at the
  347.            top, while LC chunks can skip lines.  Also, LC chunks
  348.                can skip bytes on each line.  The sign of the
  349.            byte also has opposite meanings for the two! */
  350.  
  351.         case FLI_LC:
  352.         curx = 0;
  353.         cury = get_short(cptr);     /* Lines at top to skip */
  354.         cptr += SHORTSIZE;
  355.         ndiff = get_short(cptr);    /* # of lines different */
  356.         cptr += SHORTSIZE;
  357.  
  358.         topy = cury;
  359.         bottomy = cury + ndiff;
  360.  
  361.             leftx = wwidth;
  362.             rightx = 0;
  363.  
  364.         for (k=0; k < ndiff; k++)    /* Go through each line */
  365.         {
  366.             npack = *(cptr++);       /* # packets this line */
  367.  
  368.             for (l=0; l < npack; l++)
  369.             {
  370.             skcount = *(cptr++);    /* # bytes to skip */
  371.  
  372.             if (l == 0 && skcount < leftx)
  373.                 leftx = skcount;
  374.  
  375.             curx += skcount;
  376.  
  377.             /* The number of bytes in this packet to change */
  378.  
  379.             sicount = *(cptr++);
  380.  
  381.             /* Positive, for LC means bytes the changes follow,
  382.                for BRUN means one byte to follow, spread it
  383.                through. */
  384.  
  385.             if (sicount < 128)
  386.             {
  387.                 bcopy(cptr, data + ((wwidth * cury) + curx),
  388.                   sicount);
  389.                 cptr += sicount;
  390.                 curx += sicount;
  391.             }
  392.  
  393.             else
  394.  
  395.             /* Negative, the opposite */
  396.  
  397.             {
  398.                 sicount = 256 - sicount;
  399.                 by = *(cptr++);
  400.                 dptr = data + wwidth * cury + curx;
  401.                 eptr = dptr + sicount;
  402.                 do
  403.                 *(dptr++) = by;
  404.                 while (dptr < eptr);
  405.                 curx += sicount;
  406.             }
  407.             }
  408.  
  409.             if (curx > rightx)
  410.             rightx = curx;
  411.  
  412.             /* Next line */
  413.             cury++;
  414.             curx = 0;
  415.         }
  416.  
  417.         (*func)(FLI_LC, i, data, leftx,topy, leftx,topy,
  418.             rightx-leftx,bottomy-topy);
  419.         break;
  420.  
  421.         case FLI_DELTA:
  422.         curx = 0;
  423.         cury = 0;
  424.  
  425.         by0 = *(cptr++);
  426.         by1 = *(cptr++);
  427.         ndiff = by0 + 256 * by1;     /* # of lines different */
  428.  
  429.         /* printf(" -- -- ndiff: %d  frame: %d\n",ndiff,i); */
  430.  
  431.         topy = 0;
  432.         bottomy = 0;
  433.  
  434.             leftx = wwidth;
  435.             rightx = 0;
  436.  
  437.         k=0;
  438.         while (k < ndiff)            /* Go through each line */
  439.         {
  440.             by0 = *(cptr++);
  441.             by1 = *(cptr++);
  442.             npack = by0 + 256 * by1;
  443.             if (npack >= 32768) npack -= 65536;
  444.  
  445.             /* printf(" -- npack: %d  cury: %d\n",npack,cury); */
  446.  
  447.             if (npack < 0)        /* skip lines */
  448.             {
  449.             if (cury == 0)
  450.             {
  451.                 topy = -npack;
  452.             }
  453.             cury -= npack;
  454.             continue;
  455.             }
  456.  
  457.             bottomy = cury+1;
  458.  
  459.             for (l=0; l < npack; l++)
  460.             {
  461.             skcount = *(cptr++);    /* # bytes to skip */
  462.  
  463.             if (l == 0 && skcount < leftx)
  464.                 leftx = skcount;
  465.  
  466.             curx += skcount;
  467.             /* printf("pack: %d  curx: %d  skip: %d\n",
  468.                     l,curx,skcount); */
  469.  
  470.             /* The number of words in this packet to change */
  471.  
  472.             sicount = *(cptr++);
  473.  
  474.             /* Positive, for DELTA means sicount words (16 bit)
  475.                that change follow */
  476.             /* printf("sicount: %d\n",sicount); */
  477.  
  478.             if (sicount < 128)
  479.             {
  480.                 bcopy(cptr, data + ((wwidth * cury) + curx),
  481.                   2 * sicount);
  482.                 cptr += 2 * sicount;
  483.                 curx += 2 * sicount;
  484.             }
  485.  
  486.             else
  487.  
  488.             /* Negative, the opposite */
  489.  
  490.             {
  491.                 sicount = 256 - sicount;
  492.                 by0 = *(cptr++);
  493.                 by1 = *(cptr++);
  494.                 dptr = data + wwidth * cury + curx;
  495.                 eptr = dptr + 2 * sicount;
  496.                 do
  497.                 {
  498.                 *(dptr++) = by0;
  499.                 *(dptr++) = by1;
  500.                 }
  501.                 while (dptr < eptr);
  502.                 curx += 2 * sicount;
  503.             }
  504.             }
  505.  
  506.             if (curx > rightx)
  507.             rightx = curx;
  508.  
  509.             /* Next line */
  510.             cury++;
  511.             curx = 0;
  512.             k++;
  513.         }
  514.  
  515.         /* printf(" top: %d  bot: %d    left: %d   right: %d\n",
  516.             topy, bottomy, leftx, rightx); */
  517.         (*func)(FLI_DELTA, i, data, leftx,topy, leftx,topy,
  518.             rightx-leftx,bottomy-topy);
  519.         break;
  520.  
  521.         case FLI_BRUN:
  522.         dptr = data;
  523.  
  524.         for (k=0; k < wheight; k++)    /* Go through each line */
  525.         {
  526.             npack = *(cptr++);       /* # packets this line */
  527.  
  528.             for (l=0; l < npack; l++)
  529.             {
  530.             /* The number of bytes in this packet to change */
  531.  
  532.             sicount = *(cptr++);
  533.  
  534.             /* Positive, for LC means bytes the changes follow,
  535.                for BRUN means one byte to follow, spread it
  536.                through. */
  537.  
  538.             if (sicount < 128)
  539.             {
  540.                 by = *cptr++;
  541.                 eptr = dptr + sicount;
  542.                 while (dptr < eptr)
  543.                 *(dptr++) = by;
  544.             }
  545.  
  546.             /* Negative, the opposite */
  547.  
  548.             else
  549.  
  550.             {
  551.                 sicount = 256 - sicount;
  552.                 bcopy(cptr, dptr, sicount);
  553.                 cptr += sicount;
  554.                 dptr += sicount;
  555.             }
  556.             }
  557.         }
  558.  
  559.         (*func)(FLI_BRUN, i, data, 0,0, 0,0, wwidth,wheight);
  560.         break;
  561.         }
  562.  
  563.     }
  564.  
  565.     /* Send a FLI_SYNC after each frame to get accurate timing */
  566.     (*func)(FLI_SYNC, i, (unsigned char *)0, 0,0, 0,0, 0,0);
  567.     }
  568.  
  569.     /* Only free the buffer if we allocated something */
  570.     if (!buf)
  571.     free(buf);
  572.  
  573. }
  574.